<template>
	<div>
		<el-row>
			<el-col :span="24">
				<span class="box-title">项目管理</span>
			</el-col>
		</el-row>
		<hr>
		<el-row>
			<el-col :span="24">
				<tree ref="tree" @project-add-open="projectAddOpen" 
					@project-detail-open="projectDetailOpen" @project-delete="projectDelete"></tree>
			</el-col>
		</el-row>
	</div>
	
	<!-- <div class="row">
		<div class="col-xs-12">
			<div class="portlet light bordered">
				<div class="portlet-title">
					<div class="caption">
						<span class="box-title">项目管理</span>
					</div>
				</div>
				<div class="portlet-body">
					<tree ref="tree" @project-add-open="projectAddOpen" 
					@project-detail-open="projectDetailOpen" @project-delete="projectDelete"></tree>
				</div>
			</div>
		</div>
	</div> -->
</template>

<script>
import cfg from './js/';
import store from '@/store';
import request from '@/utils/request.js';
import tree from './tree';

export default {
	store: cfg.appStore,
	components: {
		'tree': tree,
	},
	data() {
		
	},
	methods: {
		projectAddOpen: function(data) {
			this.$refs['addDeleteProject'].projectAddOpen(data);
		},
		projectDetailOpen: function(data) {
			var self = this;
			self.$store.state.main.projectSelected = self.$store.state.main.projectMap[data.entityId]
			this.$refs['projectDetail'].projectDetailOpen(self.$store.state.main.projectSelected);
		},
		authorizeUserOpen: function() {
			this.$refs['authorizeUser'].authorizeUserOpen();
		},
		projectDelete: function(data) {
		   this.$refs["addDeleteProject"].projectDelete(data);
		},
		buildingAddOpen: function() {
		   this.$refs['buildingAddEdit'].buildingAddOpen();
		},
		buildingEditOpen: function(building) {
			this.$refs['buildingAddEdit'].buildingEditOpen(building); 
		},
		setProjectTree: function(key){
			this.$refs['tree'].setProjectTree(key);
		}
	},
	mounted() {
		let self = this;
		request({
			url: '/rest/api/project/findAll',
			method: 'get'
		}).then(result => {
			var elements = result.elements;
			for(var i = 0; i < elements.length; i++) {
				var element = elements[i];
				self.$store.state.main.projectMap[element.entityId] = element;
			}
			//处理所有子项目
			var rootProjectNodes = [];
			var rootNodes = {};
			for(var i = 0; i < elements.length; i++ ){
				var element = elements[i];
				if(element.parentProjectId != null){
					var parentKey = 'project-' + element.parentProjectId;
					var node = {};
					node.type = 'project';
					node.entityId = element.entityId;
					node.parentId = element.parentProjectId;
					node.key = 'project-' + element.entityId;
					node.label = element.projectName;
					node.isHeadCompany = false;
					node.isBranchCompany = false;
					node.isHeadProject = false;
					node.isSubProject = true;
					node.parentKey = parentKey;
					node.children = null;
					if(rootNodes[element.parentProjectId] == undefined){
						rootNodes[element.parentProjectId] = [];
					}
					rootNodes[element.parentProjectId].push(node);
					var parentNode = store.state.main.treeNodeMap[parentKey];
					if(parentNode != null) {
						parentNode.children.push(node);
					} else {
						node.parentKey = 'company-' + element.organizationId;
						rootProjectNodes.push(node);
					}
					self.$store.state.main.treeNodeMap[node.key] = node;
				}
			}
			
			//处理所有主项目
			var organizationIds = [];
			var organizationNodes = {};
			var projectRootsIds = [];
			for(var i = 0; i < result.elements.length; i++) {
				var element = result.elements[i];
				if(element.parentProjectId == null){
					var node = {};
					node.type = 'project';
					node.entityId = element.entityId;
					node.parentId = element.organizationId;
					node.key = 'project-' + element.entityId;
					node.label = element.projectName;
					node.isHeadCompany = false;
					node.isBranchCompany = false;
					node.isHeadProject = true;
					node.isSubProject = false;
					node.parentKey = 'company-' + element.organizationId;
					node.children = rootNodes[element.entityId];
					rootProjectNodes.push(node);
					if(organizationNodes[element.organizationId] == undefined){
						organizationNodes[element.organizationId] = [];
					}
					organizationNodes[element.organizationId].push(node);
					if(organizationIds.indexOf(element.organizationId) == -1){
						organizationIds.push(element.organizationId);
					}
					projectRootsIds.push(element.entityId);
					self.$store.state.main.treeNodeMap[node.key] = node;
				}
			}
			
			return request({
						url: '/rest/api/organization/findByIds',
						method: 'get',
						params: {
							entityIds: organizationIds
						}
					});
		}).then(result => {
			var rootCompany = {};
			for(var i = 0; i < result.elements.length; i++) {
				var element = result.elements[i];
				if(element.parentOrganizationId != null){
					if(element.isHeadCompany || element.isBranchCompany) {
						self.$store.state.main.companyMap[element.entityId] = element;
						var node = {};
						node.type = 'company';
						node.entityId = element.entityId;
						node.parentId = element.parentOrganizationId;
						node.key = 'company-' + element.entityId;
						node.label = element.organizationName;
						node.isHeadCompany = false;
						node.isBranchCompany = true;
						node.isHeadProject = false;
						node.isSubProject = false;
						node.parentKey = null;
						node.children = organizationNodes[element.entityId];
						if(rootCompany[element.parentOrganizationId] == undefined){
							rootCompany[element.parentOrganizationId] = [];
						}
						rootCompany[element.parentOrganizationId].push(node);
						self.$store.state.main.treeNodeMap[node.key] = node;
					}
				}
			}
			for(var key in self.$store.state.main.treeNodeMap) {
				var treeNode = self.$store.state.main.treeNodeMap[key];
				if(treeNode.parentKey == null) {
					self.$store.state.main.trees.push(treeNode);
				}
			}
		}).catch(error => {
			self.$message({
				message: '加载项目数据失败：' + error.detail.msg,
				type: 'error',
				showClose: true
			});
		});
		
		request({
			url: '$/rest/api/user/findAll',
			method: 'get'
		}).then(result => {
			var elements = result.elements;
			for(var i = 0; i < elements.length; i++) {
				var element = elements[i];
				self.$store.state.main.userMap[element.entityId] = element;
			}
		}).catch(error => {
			self.$message({
				message: '加载用户数据失败：' + error.detail.msg,
				type: 'error',
				showClose: true
			});
		});
		
		request({
			url: '${ctx}/rest/api/building/findAll',
			method: 'get'
		}).then(result => {
			var elements = result.elements;
			for(var i = 0; i < elements.length; i++) {
				var element = elements[i];
				self.$store.state.main.buildingMap[element.entityId] = element;
			}
		}).catch(error => {
			self.$message({
				message: '加载楼栋数据失败：' + error.detail.msg,
				type: 'error',
				showClose: true
			});
		});
		
	},
	computed: {
		
	}
}
</script>